//
//  LXJWeiboMultyPhotosView.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/4/1.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

/// 转发微博的多图 view

/// 重用 id
let cellID = "WeiboMultyPhotosCell"
//  每项的间距
let itemMargin: CGFloat = 5
//  每项的宽度
let itemWidth: CGFloat = (UIScreen.main.bounds.size.width - CGFloat(2 * homeWeiboCellmargin) - 2 * itemMargin) / 3

class LXJWeiboMultyPhotosView: UICollectionView {
    /// 图片链接
    var pic_urls: [LXJRetweetedStatusPicttureInfo]?{
        didSet{
            /// 根据图片数量计算当前多图画布的尺寸
            calculateSize(count: pic_urls?.count ?? 0)
            /// 刷新数据源
            reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        /// CollectionView的布局
        let flowLayout = UICollectionViewFlowLayout()
        /// 调节单元格大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        /// 调整间距
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 界面搭建
    fileprivate func setupUI() -> Void {
        /// 背景颜色
        backgroundColor = UIColor.clear
        /// 注册单元格
        self.register(LXJWeiboMultyPhotosCell.self, forCellWithReuseIdentifier: cellID)
        /// 设置数据源对象
        dataSource = self
    }
    
    //  根据图片的个数计算当前配图的大小
    fileprivate func calculateSize(count: Int) {
        //  计算列数
        var cols = count > 3 ? 3 : count
        if count == 4 {
            cols = 2
        }
        //  计算行数
        let rows = (count - 1) / 3 + 1
        //  计算当前配图的宽度
        let currentWidth = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //  计算当前配图的宽度
        let currentHeight = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        //  更新约束
        self.snp.updateConstraints { (make) in
            make.size.equalTo(CGSize(width: currentWidth, height: currentHeight))
        }
    }
}

/// 实现数据源方法
extension LXJWeiboMultyPhotosView: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! LXJWeiboMultyPhotosCell
        /// 数据赋值
        cell.picsInfo = pic_urls?[indexPath.item]
        return cell
    }
}

/// 转发微博的多图 cell
class LXJWeiboMultyPhotosCell: UICollectionViewCell {
    /// 数据源对象
    var picsInfo: LXJRetweetedStatusPicttureInfo?{
        didSet{
            /// 控件赋值
            pic.sd_setImage(with: URL(string: (picsInfo?.thumbnail_pic)!), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        }
    }
    
    // MARK: 控件的懒加载
    lazy var pic: UIImageView = {
        let img = UIImageView()
        /// 设置填充模式
        img.contentMode = .scaleAspectFill
        /// 对多余部分进行裁剪
        img.clipsToBounds = true
        return img
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 添加子空间
        contentView.addSubview(pic)
        
        /// 自动布局
        pic.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
    }
}
